﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MySql.Data.MySqlClient;
using Remote.Model;

namespace Remote.Service
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“RemoteGlobalService”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务，请在解决方案资源管理器中选择 RemoteGlobalService.svc 或 RemoteGlobalService.svc.cs，然后开始调试。
    public class RemoteGlobalService : IRemoteGlobalService
    {
        public IList<ArticleInfo> GetGroupList(int groupid)
        {
            var strSql = new StringBuilder();
            strSql.Append(" select c.articleid,siteid,postdate,lastupdate,c.articlename,keywords,initial,");
            strSql.Append(" authorid,author,posterid,poster,agentid,agent,sortid,typeid,intro,notice,setting,");
            strSql.Append(" lastvolumeid,lastvolume,lastchapterid,lastchapter,chapters,size,lastvisit,dayvisit,");
            strSql.Append(" weekvisit,monthvisit,allvisit,lastvote,dayvote,weekvote,monthvote,allvote,vipvotetime,");
            strSql.Append(" vipvotenow,vipvotepreview,goodnum,badnum,toptime,saleprice,salenum,totalcost,articletype,");
            strSql.Append(" permission,firstflag,fullflag,imgflag,power,display,b.groupid,b.groupname,coverlink ");
            strSql.Append(" from metro_grouplist as a left join metro_group as b on a.groupid=b.groupid");
            strSql.Append(" left join jieqi_article_article as c on a.articleid=c.articleid");
            strSql.Append(" where a.groupid=?groupid");
            MySqlParameter[] parameters =
                {
                    new MySqlParameter("?groupid", MySqlDbType.Int32)
                };
            parameters[0].Value = groupid;
            IList<ArticleInfo> list = new List<ArticleInfo>();
            using (
                MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                               System.Data.CommandType.Text, strSql.ToString(),
                                                               parameters))
            {
                while (dr.Read())
                {
                    var model = new ArticleInfo();

                    #region

                    if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                    {
                        model.articleid = int.Parse(dr["articleid"].ToString());
                    }
                    if (dr["lastupdate"] != null && dr["lastupdate"].ToString() != "")
                    {
                        model.lastupdate = int.Parse(dr["lastupdate"].ToString());
                    }
                    if (dr["articlename"] != null)
                    {
                        model.articlename = dr["articlename"].ToString();
                    }
                    if (dr["keywords"] != null)
                    {
                        model.keywords = dr["keywords"].ToString();
                    }
                    if (dr["author"] != null)
                    {
                        model.author = dr["author"].ToString();
                    }
                    if (dr["intro"] != null)
                    {
                        model.intro = dr["intro"].ToString();
                    }
                    if (dr["lastchapterid"] != null && dr["lastchapterid"].ToString() != "")
                    {
                        model.lastchapterid = int.Parse(dr["lastchapterid"].ToString());
                    }
                    if (dr["lastchapter"] != null)
                    {
                        model.lastchapter = dr["lastchapter"].ToString();
                    }
                    if (dr["chapters"] != null && dr["chapters"].ToString() != "")
                    {
                        model.chapters = int.Parse(dr["chapters"].ToString());
                    }
                    if (dr["display"] != null && dr["display"].ToString() != "")
                    {
                        model.display = int.Parse(dr["display"].ToString());
                    }
                    model.GroupID = int.Parse(dr["groupid"].ToString());
                    model.GroupName = dr["groupname"].ToString();
                    model.coverlink = dr["coverlink"].ToString();
                    #endregion

                    list.Add(model);
                }
                return list;
            }
        }

        public IList<ArticleInfo> GetUpdateList(int count)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * from jieqi_article_article order by lastupdate limit ?count");
            //strSql.Append(" select articleid,siteid,postdate,lastupdate,c.articlename,keywords,initial,");
            //strSql.Append(" authorid,author,posterid,poster,agentid,agent,sortid,typeid,intro,notice,setting,");
            //strSql.Append(" lastvolumeid,lastvolume,lastchapterid,lastchapter,chapters,size,lastvisit,dayvisit,");
            //strSql.Append(" weekvisit,monthvisit,allvisit,lastvote,dayvote,weekvote,monthvote,allvote,vipvotetime,");
            //strSql.Append(" vipvotenow,vipvotepreview,goodnum,badnum,toptime,saleprice,salenum,totalcost,articletype,");
            //strSql.Append(" permission,firstflag,fullflag,imgflag,power,display ");
            //strSql.Append(" from jieqi_article_article ");
            //strSql.Append(" order lastupdate desc ");
            //strSql.Append(" limit ?count");
            MySqlParameter[] parameters =
                {
                    new MySqlParameter("?count", MySqlDbType.Int32)
                };
            parameters[0].Value = count;
            IList<ArticleInfo> list = new List<ArticleInfo>();
            using (
                MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                               System.Data.CommandType.Text, strSql.ToString(),
                                                               parameters))
            {
                while (dr.Read())
                {
                    var model = new ArticleInfo();

                    #region

                    if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                    {
                        model.articleid = int.Parse(dr["articleid"].ToString());
                    }
                    if (dr["lastupdate"] != null && dr["lastupdate"].ToString() != "")
                    {
                        model.lastupdate = int.Parse(dr["lastupdate"].ToString());
                    }
                    if (dr["articlename"] != null)
                    {
                        model.articlename = dr["articlename"].ToString();
                    }
                    if (dr["keywords"] != null)
                    {
                        model.keywords = dr["keywords"].ToString();
                    }
                    if (dr["author"] != null)
                    {
                        model.author = dr["author"].ToString();
                    }
                    if (dr["intro"] != null)
                    {
                        model.intro = dr["intro"].ToString();
                    }
                    if (dr["lastchapterid"] != null && dr["lastchapterid"].ToString() != "")
                    {
                        model.lastchapterid = int.Parse(dr["lastchapterid"].ToString());
                    }
                    if (dr["lastchapter"] != null)
                    {
                        model.lastchapter = dr["lastchapter"].ToString();
                    }
                    if (dr["chapters"] != null && dr["chapters"].ToString() != "")
                    {
                        model.chapters = int.Parse(dr["chapters"].ToString());
                    }
                    if (dr["display"] != null && dr["display"].ToString() != "")
                    {
                        model.display = int.Parse(dr["display"].ToString());
                    }
                    #endregion
                    //model.GroupID = int.Parse(dr["groupid"].ToString());
                    model.GroupName ="最新更新";
                    list.Add(model);
                }
                return list;
            }
        }
    }
}
